package com.module.user.ui.feedbackHistory

import android.view.View
import androidx.core.content.ContextCompat
import com.common.app.base.AppActivity
import com.common.app.data.bean.user.UserFeedbackHistoryModel
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.utils.StatusBarUtils
import com.common.base.widget.animator.CustomAnimation1
import com.module.user.R
import com.module.user.databinding.UserActivityFeedbackHistoryBinding
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/10/1
 * <p>
 * 描述：反馈历史页面
 * <p>
 * 修订历史：
 */
class FeedbackHistoryActivity : AppActivity<UserActivityFeedbackHistoryBinding, FeedbackHistoryViewModel>(),
    View.OnClickListener{

    override fun getViewModel() = FeedbackHistoryViewModel::class.java

    override fun getViewContentBinding() = UserActivityFeedbackHistoryBinding.inflate(layoutInflater)

    override fun getPageTitle(): String = ""
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    private val classicsFooter by lazy { ClassicsFooter(this) }

    private val mAdapter by lazy { FeedbackHistoryAdapter() }

    override fun initViews() {
        super.initViews()

        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(getString(R.string.user_feedback_history_title))
        headTitleUtils.setLineStyle(10)


        //上下拉刷新
        getRefreshFooter()?.let { mViewContentBinding.pdView.setRefreshFooter(it) }
        mViewContentBinding.pdView.setRefreshHeader(
            ClassicsHeader(this).setAccentColor(
                ContextCompat.getColor(this, R.color.text_content_week)
            )
        )
        classicsFooter.setAccentColor(
            ContextCompat.getColor(
                this,
                R.color.color_999999
            )
        )
        mViewContentBinding.pdView.setRefreshFooter(classicsFooter)

        mAdapter.apply {
            adapterAnimation = CustomAnimation1()
            isAnimationFirstOnly = true
            mViewContentBinding.hrv.adapter = this
        }
    }

    override fun initEvents() {
        super.initEvents()

        mViewContentBinding.pdView.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                lazyInitData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mViewContentBinding.pdView.setEnableLoadMoreWhenContentNotFull(false)
                mViewContentBinding.pdView.finishLoadMoreWithNoMoreData()
            }
        })

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
//            when(view.id){
//            }
        }
    }

    override fun lazyInitData() {
        super.lazyInitData()
        requestHistoryList()

        val mList = mutableListOf<UserFeedbackHistoryModel>()//测试数据
        mList.add(UserFeedbackHistoryModel())
        mList.add(UserFeedbackHistoryModel())
        mList.add(UserFeedbackHistoryModel())
        mAdapter.setList(mList)
    }

    override fun onClick(v: View?) {
    }

    private fun finishPdView() {
        mViewContentBinding.pdView.finishRefresh()
        mViewContentBinding.pdView.finishLoadMore()
    }

    private fun requestHistoryList() {
        showDialogLoading()
        mViewModel.requestHistoryList{
            finishPdView()
        }.observe(this) {
            finishPdView()
            dismissLoadingDialog()
            it.isSuccess().yes {
                it.Data?.let{ dataIt->
                    mAdapter.setList(dataIt)
                }
            }.otherwise {
                showToast(it.Message)
            }
        }
    }
}